package libaryinterface.user;
import libaryinterface.login.LoginInterface;
import pojo.vo.BookRecordVo;
import service.BookRecordService;
import service.BookService;
import service.UserService;
import service.impl.BookRecordServiceImpl;
import service.impl.BookServiceImpl;
import service.impl.UserServiceImpl;
import utils.TimeFormat;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.List;
//用户初始界面
public class UserInterface {
    JFrame f1;
    JPanel p1;
    JButton showbook, showlend, bBack;
    BookService bookService = new BookServiceImpl();
    UserService userService = new UserServiceImpl();
    BookRecordService bookRecordService = new BookRecordServiceImpl();
    public UserInterface(String account) {
        f1 = new JFrame("用户界面");
        //1.把图片添加到标签里（把标签的大小设为和图片大小相同），把标签放在分层面板的最底层；
        ImageIcon bg = new ImageIcon("src//picture//qwe.png");
        JLabel label = new JLabel(bg);
        label.setSize(bg.getIconWidth(),bg.getIconHeight());
        f1.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //2.把窗口面板设为内容面板并设为透明、流动布局。
        JPanel p1=(JPanel)f1.getContentPane();
        p1.setOpaque(false);
        p1.setLayout((new FlowLayout()));


        showbook = new JButton("借阅书籍");
        showlend = new JButton("查看已借书籍");
        bBack = new JButton("返回登录界面");

        p1.setLayout(null);
        showbook.setBounds(100, 100, 200, 40);
        showlend.setBounds(100, 160, 200, 40);
        bBack.setBounds(100,220,200,40);

        p1.add(showbook);
        p1.add(showlend);
        p1.add(bBack);

        f1.setVisible(true);
        f1.setBounds(770, 250, 400, 400);

        int count1=0;
        int count2=0;
        boolean flag = false;
        boolean glag = false;

        //逾期的书籍和快到期的书籍提醒
        //从数据库获得 当前用户的所有借阅记录
        List<BookRecordVo> bookRecordVoList = bookRecordService.listBookRecordByAccount(account);
        //对每条记录进行判断是否逾期
        for (BookRecordVo bookRecordVo: bookRecordVoList){
            //如果当前记录的状态为已归还,直接进行判断下一条
            if (bookRecordVo.getState().equals("是")) {
                continue;
            }
            //如果逾期就统计数量
            if( bookRecordVo.getIsOverdue().equals("是")){
                flag = true;
                count1 += bookRecordVo.getLoanNumber();
            }
            //获取当前时间
            LocalDate curTime = TimeFormat.timeFormat();
            //判断当前的日期和预计归还时间的间隔是否小于2天, 小于2天,进行统计
            if(Math.abs(bookRecordVo.getReturnTime().toEpochDay() - curTime.toEpochDay())<2){
                count2 += bookRecordVo.getLoanNumber();
                glag = true;
            }
        }
        //进行判断如果 flag 和 glag都为true则 逾期的书和 快到归还日期的书 都有
        if( flag && glag){
            JOptionPane.showMessageDialog(
                    null, "你有"+count1+"本书借阅时间已逾期\n"
                            +count2+"本书在两天内到达归还时间\n"+ "请及时处理!", "提示",
                            JOptionPane.OK_CANCEL_OPTION);
        }else if( flag){ //只有逾期的书
            JOptionPane.showMessageDialog(
                    null, "你有"+count1+"本书借阅时间已逾期\n" + "请及时处理!", "提示",
                    JOptionPane.OK_CANCEL_OPTION);
        }else if(glag ){ //只有两天内到达归还时间的书
            JOptionPane.showMessageDialog(
                    null, "你有"+count2+"本书在两天内到达归还时间\n" + "请及时归还!","提示",
                    JOptionPane.OK_CANCEL_OPTION);
        }

        //借阅按钮监听
        showbook.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    //进入用户借书界面
                    new UserLendBookInterface(account);
                    f1.dispose();
                } catch (SQLException ex) {
                    System.out.println("借书异常");
                    throw new RuntimeException(ex);
                }
            }
        });

        //归还按钮监听
        showlend.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    //进入归还书籍界面
                    new UserReturnInterface(account);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        //返回按钮监听
        bBack.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new LoginInterface();
                f1.dispose();
            }
        });
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}